// ==============================================================================================
// Group.cs
//
// Created by: NI, 2007.03.27
// Revised by: NI, 2007.11.23
// ==============================================================================================
using System.Collections.Generic;
using RibbonExplorer.Storage;
using System.Linq;

namespace RibbonExplorer.Entities
{
  /// <summary>
  /// This class represents a group on a ribbon tab.
  /// </summary>
  // ==============================================================================================
  public class Group : ItemBase
  {
    #region Private Fields

    private RibbonTab _ParentTab;
    private Dictionary<string, RibbonControl> _Controls = new Dictionary<string, RibbonControl>();

    #endregion

    #region Lifecycle methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Creates a group instance from the specified ID and name.
    /// </summary>
    /// <param name="id">Tab ID</param>
    /// <param name="parent">Parent ribbon tab</param>
    // --------------------------------------------------------------------------------------------
    public Group(string id, RibbonTab parent)
      : base(id)
    {
      _ParentTab = parent;
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the parent ribbon tab of this group.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public RibbonTab ParentTab
    {
      get { return _ParentTab; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the parent ribbon of this group. Allows that special groups overide this property.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public virtual Ribbon ParentRibbon
    {
      get { return _ParentTab.ParentRibbon;  }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the ribbon controls belonging to this group.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public Dictionary<string, RibbonControl> Controls
    {
      get { return _Controls; }
    }

    #endregion

    #region Virtual methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Obtains the child controls belonging to this group from the specified ribbon table
    /// </summary>
    /// <param name="ribbonTable">Ribbon table instance.</param>
    /// <param name="controlId">ID of the control to get its children</param>
    /// <returns>Controls belonging to the group</returns>
    // --------------------------------------------------------------------------------------------
    public virtual List<RibbonTableItem> FilterChildren(RibbonTable ribbonTable,
                                                        string controlId)
    {
      var result = from control in ribbonTable.ControlList
                                     where control.Parent == controlId
      orderby control.Ordering
      select control;
      return new List<RibbonTableItem>(result);
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Obtains the grandchild controls belonging to this group from the specified ribbon table
    /// </summary>
    /// <param name="ribbonTable">Ribbon table instance.</param>
    /// <param name="parentControlId">ID of the control to get its children</param>
    /// <param name="grandParentControlId">ID of the grandparent</param>
    /// <returns>Controls belonging to the group</returns>
    // --------------------------------------------------------------------------------------------
    public virtual List<RibbonTableItem> FilterGrandChildren(RibbonTable ribbonTable,
                                                             string parentControlId, string grandParentControlId)
    {
      return new List<RibbonTableItem>();
    }

    #endregion

    #region Overridden methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Loads the controls belonging to this group.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    protected override void OnLoadChildren()
    {
      // --- Try to obtain the ribbon table
      RibbonTable ribbonTable;
      if (RibbonTableCache.TryGetValue(_ParentTab.ParentRibbon.Id, out ribbonTable))
      {
        var controls = from control in ribbonTable.ControlList
                       where control.Type != ControlType.Tab &&
                             control.Type != ControlType.TabSet &&
                             control.Type != ControlType.Group &&
                             control.Group == Id &&
                             control.Parent == ""
                       orderby control.Ordering
                       select control;

        foreach (RibbonTableItem item in controls)
        {
          RibbonControl newControl = new RibbonControl(item.ControlId, item, this);
          if (!_Controls.ContainsKey(item.ControlId))
          {
            _Controls.Add(item.ControlId, newControl);
          }
        }
      }
    }

    #endregion
  }
}